import numpy as np
import cv2
import math
import os
from configparser import ConfigParser
import time


class pic_correct:
    
    def __init__(self, config_proportion_dir):
        
        self.proportion_dir = config_proportion_dir
        self.cut_y1 = 1000
        self.cut_y2 = 2000
        self.cut_x1 = 750
        self.cut_x2 = 2250
        self.final_result_right = 0
        self.final_result_left = 0
        
    def config(self):
        
        cp = ConfigParser()

        cp.read(self.proportion_dir)

        proportion = cp.get('proportion', 'pro')
    
        return proportion
    
    def config_label(self):
        
        cp = ConfigParser()

        cp.read(self.proportion_dir)

        self.cut_y1 = cp.get('proportion', 'y1')
        self.cut_y2 = cp.get('proportion', 'y2')
        self.cut_x1 = cp.get('proportion', 'x1')
        self.cut_x2 = cp.get('proportion', 'x2')
    
    def label(self,img):
        
        #img_label = img[1000:2000,750:2250]
        img_label = img[int(self.cut_y1):int(self.cut_y2),int(self.cut_x1):int(self.cut_x2)]
        
        #print("label_it")
        
        return img_label
    
    def rotateAntiClockWise90(self,img):# 逆时针旋转90度
        
        trans_img = cv2.transpose(img)
        
        img90 = cv2.flip(trans_img, 0)
        
        return img90
        
    def picture_shift(self, map_dir, pic_now):
        
        self.config_label()
        #def shift(imgdec1,imgdec2,template_to_dec):
            
        img1 = cv2.imread(map_dir)
        
        if img1 is None:
            print("图像为空")
            return 0
            
        imgdec1 = self.rotateAntiClockWise90(img1)
        
        size_h, size_w, size_c = imgdec1.shape
        
        imgdec2 = self.rotateAntiClockWise90(pic_now)
        
        if imgdec2 is None:
            print("图像为空")
            return 0
        
        template_to_dec = self.label(imgdec1)
    
        h, w = template_to_dec.shape[:2]
        
        #time_start = time.time()
        
        res = cv2.matchTemplate(imgdec2, template_to_dec, cv2.TM_CCORR_NORMED)


        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        
        #time_end = time.time()
        #print(time_end-time_start)

        left_top = max_loc

        right_bottom = (left_top[0] + w, left_top[1] + h)
        
        #print(left_top,right_bottom)
        
        match_y1 = left_top[1]
        match_y2 = right_bottom[1]
        match_x1 = left_top[0]
        match_x2 = right_bottom[0]
        
        cv2.rectangle(imgdec1, (int(self.cut_x1), int(self.cut_y1)), (int(self.cut_x2), int(self.cut_y2)), (0, 255, 0), 1)
        cv2.rectangle(imgdec2, (match_x1, match_y1), (match_x2, match_y2), (0, 255, 0), 1)
        
        cv2.imwrite("aresult1.jpg",imgdec1)
        cv2.imwrite("aresult2.jpg",imgdec2)

        match_medium = (match_x1+match_x2)/2
        
        medium = (int(self.cut_x1)+int(self.cut_x2))/2
                  
        shift = medium - match_medium
        
        proportion = self.config()
        
        #print(int(shift*float(proportion)))
        
        return int(shift*float(proportion))
